﻿using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Demo_串口.Service
{
    class CsINIForSp : CsINI
    {
        #region 建立单例

        private static CsINIForSp uniqueInstance;
        private static readonly object locker = new object();
        public static CsINIForSp GetInstance()
        {
            if (uniqueInstance == null)
            {
                lock (locker)
                {
                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new CsINIForSp();
                    }
                }
            }
            return uniqueInstance;
        }

        #endregion

        private static string strIniName = "ComSetting.ini";
        private string strInipath = (Directory.GetCurrentDirectory() + "\\" + strIniName);

        public List<string> SerialPortInit = new List<string>() { "COM2", "9600", "8", "1", "1" };

        public CsINIForSp()
        {
            if (!File.Exists(strInipath))
            {
                FileStream Fs = new FileStream(strInipath, FileMode.Create, FileAccess.ReadWrite);//创建写入文件
                Debug.WriteLine(strIniName + "文件建立成功");
                Fs.Close();
            }
            else
                Debug.WriteLine(strIniName + "文件已经存在");
        }

        #region ini默认写出函数

        public void WriteIniValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, strInipath);
        }

        #endregion

        #region ini默认读入为True/1

        public string ReadDefaultTrueValue(string section, string key)
        {
            StringBuilder User = new StringBuilder(512);
            GetPrivateProfileString(section, key, "", User, 512, strInipath);
            if (User.ToString() == "")
            {
                WritePrivateProfileString(section, key, "1", strInipath);
                GetPrivateProfileString(section, key, "", User, 512, strInipath);
            }
            return User.ToString();
        }

        #endregion

        #region ini默认读入为False/0

        public string ReadDefaultFalseValue(string section, string key)
        {
            StringBuilder User = new StringBuilder(512);
            GetPrivateProfileString(section, key, "", User, 512, strInipath);
            if (User.ToString() == "")
            {
                WritePrivateProfileString(section, key, "0", strInipath);
                GetPrivateProfileString(section, key, "", User, 512, strInipath);
            }
            return User.ToString();
        }

        #endregion

        #region 串口配置

        public string ReadComValue(string section, string key)
        {
            StringBuilder User = new StringBuilder(512);
            GetPrivateProfileString(section, key, "", User, 512, strInipath);
            if (User.ToString() == "")
            {
                switch (key)
                {
                    case "ComPort": WritePrivateProfileString(section, key, SerialPortInit[0], strInipath); break;
                    case "BaudRate": WritePrivateProfileString(section, key, SerialPortInit[1], strInipath); break;
                    case "DataBits": WritePrivateProfileString(section, key, SerialPortInit[2], strInipath); break;
                    case "StopBits": WritePrivateProfileString(section, key, SerialPortInit[3], strInipath); break;
                    case "Parity": WritePrivateProfileString(section, key, SerialPortInit[4], strInipath); break;
                    default: break;
                }
                GetPrivateProfileString(section, key, "", User, 512, strInipath);
            }
            return User.ToString();
        }

        #endregion
    }
}
